import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '@/views/Index/index.vue'
import $store from '@/store/index.js'


const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => {})
}
VueRouter.prototype.replace = function replace(location) {
	return originalReplace.call(this, location).catch(err => {})
};

Vue.use(VueRouter)

const routes = [{
		path: "/login",
		component: () => import("@/views/Login/login.vue"),
		name: "Login"
	},
	{
		path: "/",
		component: Index,
		redirect: '/dashboard',
		children: [{
				path: '/dashboard',
				name: 'Dashboard',
				component: () => import("@/views/Dashboard/dashboard.vue"),
				meta: {
					title: '仪表盘',
					tab: [{
						text: '仪表盘'
					}]
				}
			},
			{
				path: '/404',
				name: '404',
				component: () => import("@/views/404.vue"),
				meta: {
					tab: [{
							text: '首页',
							to: '/dashboard'
						},
						{
							text: '404'
						}
					]
				}
			}, {
				path: '/test',
				name: 'test',
				component: () => import("@/views/Test/test.vue"),
				meta: {
					tab: [{
							text: '首页',
							to: '/dashboard'
						},
						{
							text: '测试'
						}
					]
				}
			}, {
				path: '/userinfo',
				name: 'userinfo',
				component: () => import("@/views/User/userinfo.vue"),
				meta: {
					tab: [{
							text: '首页',
							to: '/dashboard'
						},
						{
							text: '用户信息'
						}
					]
				}
			}
		]
	}
]

import setting from './setting.js'
import log from './log.js'
import other from './other.js'
import image from './image.js'
routes[1].children.push(...setting)
routes[1].children.push(...log)
routes[1].children.push(...other)
routes[1].children.push(...image)

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})
router.beforeEach((to, from, next) => {
	if (to.matched.length !== 0) {
		//如果用户没有登录，并且访问的页面不是登录页，就重定向到登录页
		next()
	} else {
		next({
			path: '/404?path=' + to.path
		})
	}
});
export default router
